/**
 * Created by 追风少年 on 2018/6/7.
 */

var ServiceModelInput = {
    URL:{
        ADD_URL:'/admin/serviceModel/add',
        UPDATE_URL:'/admin/serviceModel/update',
        FIND_ALL_ROLE_URL:"/admin/role/findAllRole",
        FIND_ONE_URL:"/admin/serviceModel/findById"
    }
};

app.controller('inputServiceModelController', function($scope,$http,$q) {

    $scope.allRoles = [];

    $scope.entity = {
        service_name:"",
        role_id:""
    };
    $scope.submitUrl = ServiceModelInput.URL.ADD_URL;

    // 页面加载完成
    $scope.load = function(){
        var id = $("#serviceId").val();
        if(id != "none"){ // is update page
            $scope.submitUrl = ServiceModelInput.URL.UPDATE_URL;
        }
    };

    $scope.validateParam = function(){
        $scope.entity.role_id = $("#roleSelect").val();
        if(Utils.isNull($scope.entity.service_name)){
            layer.msg("请填写服务名称");
            return false;
        }
        if(Utils.isNull($scope.entity.role_id)){
            layer.msg("请选择服务执行部门");
            return false;
        }
        return true;
    };

    $scope.submit = function($event){
        if(!$scope.validateParam()){ // 校验参数
            return false;
        }
        var btn = $event.target;
        btn.disabled = true;
        console.log($scope.entity);
        $http.post($scope.submitUrl,$scope.entity).success(function (ret) {
            console.log(ret);
            if(ret.status){
                layer.alert(ret.msg, {
                    icon: 1,
                    skin: 'layer-ext-moon',
                    yes:function(index){
                        layer.close(index);
                        parent.closeWinAndFlush();
                    }
                });
            }else{
                btn.disabled = false;
                console.warn(ret);
                layer.msg(ret.msg);
                return;
            }
        }).error(function(ret){
            btn.disabled = false;
            console.error("err:"+ret);
        });

    };


    /********** 构建初始数据 **********/
    // 获取所有的角色
    function getAllRole() {
        return $q(function (resolve, reject) {
            $http.post(ServiceModelInput.URL.FIND_ALL_ROLE_URL,{}).success(function(data,status,headers,config){
                console.log(data);
                if(data.status){
                    $scope.allRoles = data.data;
                    resolve("getAllRole successful");
                }else{
                    console.error(data);
                    reject("getAllRole fail");
                    alert("数据加载失败,请联系管理员");
                }
            }).error(function(data,status,headers,config){
                console.error(data);
                reject("getAllRole fail");
            });
        })
    }

    // 获取编辑的用户数据
    function getServiceInfo() {
        return $q(function (resolve, reject) {
            var id = $("#serviceId").val();
            if(id == "none"){
                resolve("getUserInfo successful");
                return false;
            }
            $http.post(ServiceModelInput.URL.FIND_ONE_URL,{id:id}).success(function(data,status,headers,config){
                console.log("ServiceModelInput find one 数据请求成功",data);
                if(data.status){
                    $scope.entity = data.data;
                    resolve("ServiceModelInput find one successful");
                }else{
                    console.error(data);
                    reject("ServiceModelInput find one fail");
                    alert("数据加载失败,请联系管理员");
                }
            }).error(function(data,status,headers,config){
                console.error(data);
                reject("ServiceModelInput find one fail");
            });
        });
    }

    $q.all([getAllRole(), getServiceInfo()])
        .then(function (dataArr) {
            //promise都成功执行后的回调函数
            console.log("$q.all: ", dataArr);
            var select = $("#roleSelect");
            var roles = $scope.allRoles;
            for(var i = 0; i < roles.length; i++){
                if(roles[i].roleid == $scope.entity.role_id){
                    select.append('<option  selected="" value="'+roles[i].roleid+'">'+roles[i].rolename+'</option>');
                }else{
                    select.append('<option value="'+roles[i].roleid+'">'+roles[i].rolename+'</option>');
                }
            }
            layui.use('form', function(){
                var form = layui.form;
                form.render('select'); //刷新select选择框渲染
            });
        }, function (err) {
            console.log("$q.all: ", err)
        });


});